package cn.iocoder.yudao.module.team.controller.admin.financemonad;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.financemonad.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.financemonad.FinanceMonadDO;
import cn.iocoder.yudao.module.team.service.financemonad.FinanceMonadService;

@Tag(name = "管理后台 - 录入单子管理")
@RestController
@RequestMapping("/team/finance-monad")
@Validated
public class FinanceMonadController {

    @Resource
    private FinanceMonadService financeMonadService;

    @PostMapping("/create")
    @Operation(summary = "创建录入单子管理")
    @PreAuthorize("@ss.hasPermission('team:finance-monad:create')")
    public CommonResult<Integer> createFinanceMonad(@Valid @RequestBody FinanceMonadSaveReqVO createReqVO) {
        return success(financeMonadService.createFinanceMonad(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新录入单子管理")
    @PreAuthorize("@ss.hasPermission('team:finance-monad:update')")
    public CommonResult<Boolean> updateFinanceMonad(@Valid @RequestBody FinanceMonadSaveReqVO updateReqVO) {
        financeMonadService.updateFinanceMonad(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除录入单子管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:finance-monad:delete')")
    public CommonResult<Boolean> deleteFinanceMonad(@RequestParam("id") Integer id) {
        financeMonadService.deleteFinanceMonad(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除录入单子管理")
                @PreAuthorize("@ss.hasPermission('team:finance-monad:delete')")
    public CommonResult<Boolean> deleteFinanceMonadList(@RequestParam("ids") List<Integer> ids) {
        financeMonadService.deleteFinanceMonadListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得录入单子管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:finance-monad:query')")
    public CommonResult<FinanceMonadRespVO> getFinanceMonad(@RequestParam("id") Integer id) {
        FinanceMonadDO financeMonad = financeMonadService.getFinanceMonad(id);
        return success(BeanUtils.toBean(financeMonad, FinanceMonadRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得录入单子管理分页")
    @PreAuthorize("@ss.hasPermission('team:finance-monad:query')")
    public CommonResult<PageResult<FinanceMonadRespVO>> getFinanceMonadPage(@Valid FinanceMonadPageReqVO pageReqVO) {
        PageResult<FinanceMonadDO> pageResult = financeMonadService.getFinanceMonadPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FinanceMonadRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出录入单子管理 Excel")
    @PreAuthorize("@ss.hasPermission('team:finance-monad:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFinanceMonadExcel(@Valid FinanceMonadPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FinanceMonadDO> list = financeMonadService.getFinanceMonadPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "录入单子管理.xls", "数据", FinanceMonadRespVO.class,
                        BeanUtils.toBean(list, FinanceMonadRespVO.class));
    }

}